#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>

#include "Constant.h"

char HOSTIP[INET_ADDRSTRLEN];
char BROADCASTIP[INET_ADDRSTRLEN];
int HOSTID;

char interface[] = "rausb0";

void constant_init()
{
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	struct ifreq ifr;
	
	bzero(&ifr, sizeof(ifr));
	strcpy(ifr.ifr_name, interface);

	//Get Broadcast IP
	if(-1 == ioctl(sockfd, SIOCGIFBRDADDR, &ifr))
	{
		perror("udpClient::broadcast ioctl()");
		exit(-1);
	}

	inet_ntop(AF_INET, &((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr, BROADCASTIP, INET_ADDRSTRLEN);
	
	//Get Wireless interface IP
	if(-1 == ioctl(sockfd, SIOCGIFADDR, &ifr))
	{
		perror("udpClient::broadcast ioctl()");
		exit(-1);
	}

	inet_ntop(AF_INET, &((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr, HOSTIP, INET_ADDRSTRLEN);
	
	//Get Host ID
	char *p = HOSTIP;
	int i;
	for(i=0;i<3;++i)
		p = strchr(p, '.');
	
	sscanf(p+1, "%d", &HOSTID);
}
